/**
 * This class was added by the Hash Hush team 
 */

package com.prpat;

import de.uniba.wiai.lspi.chord.console.command.RemoteChordNetworkAccess;
import de.uniba.wiai.lspi.chord.service.Chord;
import de.uniba.wiai.lspi.chord.service.Report;
import de.uniba.wiai.lspi.util.console.Command;

public class FindSuccessor extends Command
{
	/**
	 * The name of this {@link Command}.
	 */
	public static final String COMMAND_NAME = "find";
	
	private static String SUCC_NAME;

	/** Creates a new instance of FindSuccessor
	 * @param toCommand1 
	 * @param out1 */
	public FindSuccessor(Object[] toCommand1, java.io.PrintStream out1)
	{
		super(toCommand1, out1);
		SUCC_NAME = new String("");
	}
	
	/**
	 * @param node
	 */
	private void printFingerTableForEndpoint(Report node) 
	{
		SUCC_NAME = new String(node.printSuccessorList());
	}
    
	public void exec() 
	{
		Chord instance = ((RemoteChordNetworkAccess) this.toCommand[1]).getChordInstance();
		if(instance != null)
			this.printFingerTableForEndpoint((Report)(instance));
		else
			SUCC_NAME = null;
			
	}

	public String getCommandName() {
		return COMMAND_NAME;
	}

	public void printOutHelp() {
		this.out.println("This command searches for successor of the node");
	}

	
	public String toString()
	{
		return (SUCC_NAME==null || SUCC_NAME.equalsIgnoreCase("Successor List:\n")) ? null : SUCC_NAME;
	}
}
